<template>
  <view>
    <view class="my-settle-container">
        <label class="radio" @click="changeAllState">
          <radio :checked="isFullCheck" color="#1296db" /> <text>全选</text>
        </label>
        <view class="amount-box">
          合计：<text class="amount">￥{{checkedGoodsAmount}}</text>
        </view>
        <view class="btn-settle" @click="settlement">
          结算({{checkedCount}})
        </view>
    </view>
  </view>
</template>

<script>
  import { mapGetters,mapMutations,mapState } from 'vuex'
  export default {
    name:"my-swttle",
    data() {
      return {
        seconds:3,
        timer:null
      };
    },
    methods:{
      ...mapMutations('m_cart',["updateAllGoodsState",'emptyCart']),
      ...mapMutations('m_user',['updateRedirectInfo']),
      changeAllState(){
        // console.log("22")
        this.updateAllGoodsState(!this.isFullCheck)
      },
      settlement(){
        if(!this.checkedCount) return uni.showMsg("请选择结算商品")
        if(!this.addstr) return uni.showMsg("请选择收货地址")
        // if(!this.token) return uni.showMsg('请先登录！')
        if(!this.token) return this.delayNavigate()
        //实现支付功能
        this.payOrder()
        
      },
      showTips(n){
        uni.showToast({
          icon:'none',
          title:'请登录后再结算！' + n + ' 秒后自动跳转到登录页',
          mask:true,
          duration:1500
        })
      },
      delayNavigate(){
        this.seconds=3
        // 1. 展示提示消息，此时 seconds 的值等于 3
          this.showTips(this.seconds)
        console.log("timer")
          // 2. 创建定时器，每隔 1 秒执行一次
          this.timer=setInterval(() => {
            // 2.1 先让秒数自减 1
            this.seconds--
            if(this.seconds<=0){
              clearInterval(this.timer)
              uni.switchTab({
                url:"/pages/my/my",
                success:()=> {
                  this.updateRedirectInfo({
                    openType:'switchTab',
                    from:'/pages/cart/cart'
                  })
                }
              })
              return
            }
            // 2.2 再根据最新的秒数，进行消息提示
            this.showTips(this.seconds)
          }, 1000)
      },
      //微信支付
      async payOrder(){
        // console.log(this.cart)
        const orderInfo={
          order_price:0.01,
          consignee_addr:this.addstr,
     goods: this.cart.filter(x => x.goods_state).map(x => ({ goods_id: x.goods_id, goods_number: x.goods_count, goods_price: x.goods_price })) 
          /* goods:this.cart.filter(x=>x.goods_state).map(x=>({ goods_id: x.goods_id, goods_number: x.goods_count, goods_price: x.goods_price })) */
        }
         // console.log(orderInfo)
        const {data:res}=await uni.$http.post('/api/public/v1/my/orders/create',orderInfo)
        if(res.meta.status!==401)return uni.showMsg('创建订单失败！')
        const orderNumber="GD20180507000000000050"
        // 2.1 发起请求获取订单的支付信息
          const { data: res2 } = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', { order_number: orderNumber })
          if (res2.meta.status !== 401) return uni.$showError('预付订单生成失败！')
           const payInfo ={
             timeStamp: "1525681145",
                   nonceStr: "BkPggorBXZwPGXe3",
                   package: "prepay_id=wx071619042918087bb4c1d3d72999385683",
                   signType: "MD5",
                   paySign: "D1642DEEF1663C8012EDEB9297E1D516"
           }
          // console.log(res2)
          const [err, succ] = await uni.requestPayment(payInfo)
         if(!err) return uni.$showMsg('订单未支付！')
            // 3.3 完成了支付，进一步查询支付的结果
             const { data: res3 } = await uni.$http.post('/api/public/v1/my/orders/chkOrder', { order_number: orderNumber })
            // 3.4 检测到订单未支付
            console.log(res3)
           if (res3.meta.status !== 401) return uni.showMsg('订单未支付！')
            // 3.5 检测到订单支付完成
            uni.showToast({
              title: '支付完成！',
              icon: 'success'
            })
            this.emptyCart([])
      }
    },
    computed:{
      ...mapGetters('m_cart',['checkedCount','total','checkedGoodsAmount']),
      ...mapGetters('m_user',["addstr"]),
      ...mapState('m_user',['token']),
      ...mapState('m_cart',['cart']),
      isFullCheck(){
        return this.total===this.checkedCount
      }
    }
  }
</script>

<style lang="scss">
.my-settle-container{
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 50px;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 10rpx;
  font-size: 28rpx;
  .radio{
    display: flex;
    align-items: center;
  }
  .amount{
    color:#C00000;
  }
   .btn-settle {
      height: 50px;
      min-width: 100px;
      background-color: #c00000;
      color: white;
      line-height: 50px;
      text-align: center;
      padding: 0 10px;
    }
}
</style>
